﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Stocks;
using System.Data.SqlClient;
using System.Data;
using ChangeHope.DataBase;

namespace YXShop.SQLServerDAL.Stocks
{
    public class BarCode:IBarCode
    {
        #region IBarCode 成员
        #region DataBase Operation
        public int Add(Model.Stocks.BarCode model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_barcode(");
            strSql.Append("barcode,productid,stocktypeid,stockid,intime,outtime,stateid,notes)");
            strSql.Append(" values (");
            strSql.Append("@barcode,@productid,@stocktypeid,@stockid,@intime,@outtime,@stateid,@notes)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@barcode", SqlDbType.VarChar,40),
					new SqlParameter("@productid", SqlDbType.Int,4),
					new SqlParameter("@stocktypeid", SqlDbType.Int,4),
					new SqlParameter("@stockid", SqlDbType.Int,4),
					new SqlParameter("@intime", SqlDbType.DateTime),
					new SqlParameter("@outtime", SqlDbType.DateTime),
					new SqlParameter("@stateid", SqlDbType.Int,4),
					new SqlParameter("@notes", SqlDbType.Text)};
     
            parameters[0].Value = model.Barcode;
            parameters[1].Value = model.Productid;
            parameters[2].Value = (int)model.StockTypeID;
            parameters[3].Value = model.StockID;
            parameters[4].Value = model.InTime;
            parameters[5].Value = model.OutTime;
            parameters[6].Value = (int)model.StateID;
            parameters[7].Value = model.Notes;

            object obj = SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
       
        public int Update(Model.Stocks.BarCode model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_barcode set ");
            strSql.Append("barcode=@barcode,");
            strSql.Append("productid=@productid,");
            strSql.Append("stocktypeid=@stocktypeid,");
            strSql.Append("stockid=@stockid,");
            strSql.Append("intime=@intime,");
            strSql.Append("outtime=@outtime,");
            strSql.Append("stateid=@stateid,");
            strSql.Append("notes=@notes");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@barcode", SqlDbType.VarChar,40),
					new SqlParameter("@productid", SqlDbType.Int,4),
					new SqlParameter("@stocktypeid", SqlDbType.Int,4),
					new SqlParameter("@stockid", SqlDbType.Int,4),
					new SqlParameter("@intime", SqlDbType.DateTime),
					new SqlParameter("@outtime", SqlDbType.DateTime),
					new SqlParameter("@stateid", SqlDbType.Int,4),
					new SqlParameter("@notes", SqlDbType.Text)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Barcode;
            parameters[2].Value = model.Productid;
            parameters[3].Value = (int)model.StockTypeID;
            parameters[4].Value = model.StockID;
            parameters[5].Value = model.InTime;
            parameters[6].Value = model.OutTime;
            parameters[7].Value = (int)model.StateID;
            parameters[8].Value = model.Notes;

            return SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_barcode ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_barcode] where id in(");
            strSql.Append(" " + ids + " )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }
        #endregion
        #region DataBase Load
        public Model.Stocks.BarCode GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,barcode,productid,stocktypeid,stockid,intime,outtime,stateid,notes from yxs_barcode ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.Stocks.BarCode model = new Model.Stocks.BarCode();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Barcode = ds.Tables[0].Rows[0]["barcode"].ToString();
                if (ds.Tables[0].Rows[0]["productid"].ToString() != "")
                {
                    model.Productid = int.Parse(ds.Tables[0].Rows[0]["productid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stocktypeid"].ToString() != "")
                {
                    model.StockTypeID = (Model.Stocks.StockTypeEnum)int.Parse(ds.Tables[0].Rows[0]["stocktypeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockid"].ToString() != "")
                {
                    model.StockID = int.Parse(ds.Tables[0].Rows[0]["stockid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["intime"].ToString() != "")
                {
                    model.InTime = DateTime.Parse(ds.Tables[0].Rows[0]["intime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["outtime"].ToString() != "")
                {
                    model.OutTime = DateTime.Parse(ds.Tables[0].Rows[0]["outtime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stateid"].ToString() != "")
                {
                    model.StateID = (Model.Stocks.ProductStateEnum)int.Parse(ds.Tables[0].Rows[0]["stateid"].ToString());
                }
                model.Notes = ds.Tables[0].Rows[0]["notes"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        public Model.Stocks.BarCode GetModelByBarCode(string barcode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,barcode,productid,stocktypeid,stockid,intime,outtime,stateid,notes from yxs_barcode ");
            strSql.Append(" where barcode=@barcode ");
            SqlParameter[] parameters = {
					new SqlParameter("@barcode", SqlDbType.VarChar,40)};
            
            parameters[0].Value = barcode;

            Model.Stocks.BarCode model = new Model.Stocks.BarCode();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Barcode = ds.Tables[0].Rows[0]["barcode"].ToString();
                if (ds.Tables[0].Rows[0]["productid"].ToString() != "")
                {
                    model.Productid = int.Parse(ds.Tables[0].Rows[0]["productid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stocktypeid"].ToString() != "")
                {
                    model.StockTypeID = (Model.Stocks.StockTypeEnum)int.Parse(ds.Tables[0].Rows[0]["stocktypeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockid"].ToString() != "")
                {
                    model.StockID = int.Parse(ds.Tables[0].Rows[0]["stockid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["intime"].ToString() != "")
                {
                    model.InTime = DateTime.Parse(ds.Tables[0].Rows[0]["intime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["outtime"].ToString() != "")
                {
                    model.OutTime = DateTime.Parse(ds.Tables[0].Rows[0]["outtime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stateid"].ToString() != "")
                {
                    model.StateID = (Model.Stocks.ProductStateEnum)int.Parse(ds.Tables[0].Rows[0]["stateid"].ToString());
                }
                model.Notes = ds.Tables[0].Rows[0]["notes"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        public ChangeHope.DataBase.DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_barcode [where] 1=1 ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("" + strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage(false);
            return dataPage;
        }

        public List<Model.Stocks.BarCode> GetListBarCodeByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,barcode,productid,stocktypeid,stockid,intime,outtime,stateid,notes ");
            strSql.Append(" FROM yxs_barcode ");
            if (where.Trim() != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds=SQLServerHelper.Query(strSql.ToString());
            List<Model.Stocks.BarCode> modelList = new List<Model.Stocks.BarCode>();
            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                int rowsCount = dt.Rows.Count;
                if (rowsCount > 0)
                {
                    Model.Stocks.BarCode model;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        model = new Model.Stocks.BarCode();
                        if (dt.Rows[n]["id"].ToString() != "")
                        {
                            model.ID = int.Parse(dt.Rows[n]["id"].ToString());
                        }
                        model.Barcode = dt.Rows[n]["barcode"].ToString();
                        if (dt.Rows[n]["productid"].ToString() != "")
                        {
                            model.Productid = int.Parse(dt.Rows[n]["productid"].ToString());
                        }
                        if (dt.Rows[n]["stocktypeid"].ToString() != "")
                        {
                            model.StockTypeID = (Model.Stocks.StockTypeEnum)int.Parse(dt.Rows[n]["stocktypeid"].ToString());
                        }
                        if (dt.Rows[n]["stockid"].ToString() != "")
                        {
                            model.StockID = int.Parse(dt.Rows[n]["stockid"].ToString());
                        }
                        if (dt.Rows[n]["intime"].ToString() != "")
                        {
                            model.InTime = DateTime.Parse(dt.Rows[n]["intime"].ToString());
                        }
                        if (dt.Rows[n]["outtime"].ToString() != "")
                        {
                            model.OutTime = DateTime.Parse(dt.Rows[n]["outtime"].ToString());
                        }
                        if (dt.Rows[n]["stateid"].ToString() != "")
                        {
                            model.StateID = (Model.Stocks.ProductStateEnum)int.Parse(dt.Rows[n]["stateid"].ToString());
                        }
                        model.Notes = dt.Rows[n]["notes"].ToString();
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_barcode [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion
        #endregion

      
        #region Other Function
        public IDbDataParameter[] ValueParas(YXShop.Model.Stocks.BarCode model)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@barcode", SqlDbType.VarChar,40),
					new SqlParameter("@productid", SqlDbType.Int,4),
					new SqlParameter("@stocktypeid", SqlDbType.Int,4),
					new SqlParameter("@stockid", SqlDbType.Int,4),
					new SqlParameter("@intime", SqlDbType.DateTime),
					new SqlParameter("@outtime", SqlDbType.DateTime),
					new SqlParameter("@stateid", SqlDbType.Int,4),
					new SqlParameter("@notes", SqlDbType.Text)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Barcode;
            parameters[2].Value = model.Productid;
            parameters[3].Value = (int)model.StockTypeID;
            parameters[4].Value = model.StockID;
            parameters[5].Value = model.InTime;
            parameters[6].Value = model.OutTime;
            parameters[7].Value = (int)model.StateID;
            parameters[8].Value = model.Notes;

            return parameters;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataSetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,barcode,productid,stocktypeid,stockid,intime,outtime,stateid,notes ");
            strSql.Append(" FROM yxs_barcode ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString());
        }
        #endregion
    }
}
